package com.itbaizhan;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

//用配置文件连接mongodb
public class MongodbExample2 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            /*前面三步通用，获取properties 的值*/
            //使用classloader 加载properties 文件获取对应的输入流
            InputStream in =MongodbExample2.class.getClassLoader().getResourceAsStream("config.properties");
            //使用properties加载输入流
            properties.load(in);
            //获取key的value
            String connectionString = properties.getProperty("mongodb.uri");

            //获取mongoClient 对象
            MongoClient mongoClient = MongoClients.create(connectionString);
           // ArrayList<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());
            //databases.forEach(db -> System.out.println(db.toJson()));

            //创建集合
            //获取数据库
            MongoDatabase mydb = mongoClient.getDatabase("mydb");
            mydb.createCollection("exampleCollection");


        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
